/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaHabitaciones.java
 *
 * Created on 12/11/2010, 10:46:42 PM
 */

package pe.edu.upla.lamerced.desk.view.maestro;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Habitacion;
import pe.edu.upla.lamerced.service.HabitacionService;

/**
 *
 * @author elvis
 */
public class ListaHabitaciones extends javax.swing.JInternalFrame {

   private List<Habitacion> listaHabitaciones;
   private HabitacionService habitacionService;
   private JDesktopPane escitorio;
    /** Creates new form ListaHabitaciones */
    public ListaHabitaciones() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblHabitaciones = new javax.swing.JTable();
        btnNew = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btneliminar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Lista de Habitaciones");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Mantenimiento de habitaciones");

        tblHabitaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Categoria", "Tipo", "Numero", "Piso", "Estado"
            }
        ));
        jScrollPane1.setViewportView(tblHabitaciones);

        btnNew.setText("Nueva");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        btnedit.setText("Modificar");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btneliminar.setText("Eliminar");
        btneliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(78, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnNew)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnedit)
                        .addGap(45, 45, 45)
                        .addComponent(btneliminar)
                        .addGap(23, 23, 23))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(59, 59, 59))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNew)
                    .addComponent(btneliminar)
                    .addComponent(btnedit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditActionPerformed
       if (tblHabitaciones.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado una habitacion");
         return;
       }
       FormHabitacion form = new FormHabitacion();
       form.setHabitacion(listaHabitaciones.get(tblHabitaciones.getSelectedRow()));
       escitorio.add(form);
       form.cargaInicial();
       form.setEscritorio(escitorio);
       form.setVisible(true);
       this.setVisible(false);
    }//GEN-LAST:event_btneditActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
       // TODO add your handling code here:
       FormHabitacion form = new FormHabitacion();
       form.setHabitacion(new Habitacion());
       form.cargaInicial();
       form.setEscritorio(escitorio);
       escitorio.add(form);
       form.setVisible(true);
       this.setVisible(false);
    }//GEN-LAST:event_btnNewActionPerformed

    private void btneliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneliminarActionPerformed
        // TODO add your handling code here:
         if (tblHabitaciones.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(this, "No ha seleccionado ninguna habitacion");
         return;
       }
       int res = JOptionPane.showConfirmDialog(this,"Seguro que desea eliminar la habitacion?","Eliminar Habitacion",JOptionPane.YES_NO_OPTION);
       if(res==0){
         Habitacion dt = listaHabitaciones.get(tblHabitaciones.getSelectedRow());
         dt.setEstado(0);
         habitacionService.modificarHabitacion(dt);
         JOptionPane.showMessageDialog(this, "se ha eliminado la habitacion seleccionada");
         listaHabitaciones = habitacionService.listaHabitaciones();
         pintagrilla();
       }
    }//GEN-LAST:event_btneliminarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btneliminar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblHabitaciones;
    // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(habitacionService==null){
         habitacionService = (HabitacionService)Inicial.getFactory().getBean("habitacionService");
      }
      listaHabitaciones = habitacionService.listaHabitaciones();
      pintagrilla();
   }
   public void pintagrilla(){
     DefaultTableModel tabla = (DefaultTableModel) tblHabitaciones.getModel();
     tabla.setRowCount(0);
     for (Habitacion hab : listaHabitaciones) {
         Object[] fila = new Object[6];
         fila[0] = hab.getIdhabitacion();
         fila[1] = hab.getCategoria().getNombre();
         fila[2] = hab.getTipohabitacion().getNombre();
         fila[3] = hab.getNumero();
         fila[4] = hab.getPiso();
         fila[5] = hab.getEstadoLetra();
         tabla.addRow(fila);
     }
    }

   public JDesktopPane getEscitorio() {
      return escitorio;
   }
   public void setEscitorio(JDesktopPane escitorio) {
      this.escitorio = escitorio;
   }
}
